package cn.iocoder.yudao.module.member.api.company;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.member.api.company.dto.CompanyPageReqDTO;
import cn.iocoder.yudao.module.member.api.company.dto.CompanyRespDTO;
import cn.iocoder.yudao.module.member.api.company.dto.CompanyUpdateReqDTO;
import cn.iocoder.yudao.module.member.api.company.dto.CompanyUserRespDTO;
import cn.iocoder.yudao.module.member.controller.admin.company.vo.CompanyPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.company.vo.CompanyUpdateReqVO;
import cn.iocoder.yudao.module.member.controller.admin.company.vo.CompanyUserPageReqVO;
import cn.iocoder.yudao.module.member.convert.company.CompanyConvert;
import cn.iocoder.yudao.module.member.convert.company.CompanyUserConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.company.CompanyDO;
import cn.iocoder.yudao.module.member.dal.dataobject.company.CompanyUserDO;
import cn.iocoder.yudao.module.member.dal.dataobject.job.MemberCompanyJobDO;
import cn.iocoder.yudao.module.member.service.company.CompanyService;
import cn.iocoder.yudao.module.member.service.job.MemberJobService;
import cn.iocoder.yudao.module.system.dal.redis.company.CompanyRedisDAO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 企业 API 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CompanyApiImpl implements CompanyApi {

    @Resource
    private CompanyService companyService;
    @Resource
    private CompanyRedisDAO companyRedisDAO;

    @Resource
    private MemberJobService memberJobService;
    @Override
    public CompanyRespDTO getCompanyByTenantId(Long id) {
        CompanyRespDTO companyRespDTO=new CompanyRespDTO();
        CompanyDO companyDO=companyService.getCompanyByTenantId(id);
        if(companyDO==null){
            return companyRespDTO;
        }
        BeanUtils.copyProperties(companyDO,companyDO);
        return companyRespDTO;
    }

    @Override
    public PageResult<CompanyRespDTO> getCompanyPage(CompanyPageReqDTO pageVO) {
        CompanyPageReqVO companyPageReqVO=new CompanyPageReqVO();
        Duration d = Duration.ofDays(1);
        BeanUtils.copyProperties(pageVO,companyPageReqVO);
        PageResult<CompanyDO> companyDOPageResult= companyService.getCompanyPage(companyPageReqVO);
        List<CompanyRespDTO> companyRespDTOList=CompanyConvert.INSTANCE.convertList1(companyDOPageResult.getList());
        String cardNo="";
        for (CompanyRespDTO companyRespDTO:companyRespDTOList) {
//            String jobJson=companyRedisDAO.get(companyRespDTO.getId().toString());//获取缓存
//            if(StringUtils.isEmpty(jobJson)){
                List<MemberCompanyJobDO>  memberCompanyJobDOList=memberJobService.getJobListTop2(companyRespDTO.getId());
                String jobJson=JsonUtils.toJsonString(memberCompanyJobDOList);
                companyRedisDAO.set(companyRespDTO.getId().toString(),jobJson,d);
//            }
            //有则json对象 设置进去
            companyRespDTO.setCompanyJobDOList(jobJson);
            cardNo=companyRespDTO.getCardNo();
            if(StringUtils.isNotBlank(cardNo)){
                companyRespDTO.setCardNo(cardNo.substring(0,4)+"****"+cardNo.substring(14));
            }
        }
        PageResult<CompanyRespDTO>  pageResult=new PageResult<CompanyRespDTO>();
        pageResult.setList(companyRespDTOList);
        pageResult.setTotal(companyDOPageResult.getTotal());
        return pageResult;
    }

    @Override
    public List<CompanyRespDTO> getHotCompanyPage(CompanyPageReqDTO pageVO) {
        CompanyPageReqVO companyPageReqVO=new CompanyPageReqVO();
        Duration d = Duration.ofDays(1);
        BeanUtils.copyProperties(pageVO,companyPageReqVO);
        List<CompanyDO> companyDOPageResult=companyService.getHotCompanyPage(companyPageReqVO);
        List<CompanyRespDTO> companyRespDTOList=CompanyConvert.INSTANCE.convertList1(companyDOPageResult);
        for (CompanyRespDTO companyRespDTO:companyRespDTOList) {
//            String jobJson=companyRedisDAO.get(companyRespDTO.getId().toString());//获取缓存
//            if(StringUtils.isEmpty(jobJson)){
               List<MemberCompanyJobDO>  memberCompanyJobDOList=memberJobService.getJobListTop2(companyRespDTO.getId());
            String  jobJson=JsonUtils.toJsonString(memberCompanyJobDOList);
                companyRedisDAO.set(companyRespDTO.getId().toString(),jobJson,d);
//            }
            //有则json对象 设置进去
            companyRespDTO.setCompanyJobDOList(jobJson);
        }

        //插入职位信息
        return companyRespDTOList;
    }
    @Override
    public List<CompanyRespDTO> getHotCompanyPage1(CompanyPageReqDTO pageVO) {
        CompanyPageReqVO companyPageReqVO=new CompanyPageReqVO();
        Duration d = Duration.ofDays(1);
        BeanUtils.copyProperties(pageVO,companyPageReqVO);
        List<CompanyDO> newList=new ArrayList<>();
        List<CompanyDO> companyHot = companyService.getCompanyByIsHot();
        if(CollectionUtils.isNotEmpty(companyHot)){
            List<Long> ids = companyHot.stream().map(CompanyDO::getId).collect(Collectors.toList());
            companyPageReqVO.setIds(ids);
            companyPageReqVO.setPageSize(50-ids.size());
            newList.addAll(companyHot);
        }
        List<CompanyDO> companyDOPageResult=companyService.getHotCompanyPage(companyPageReqVO);
        if(CollectionUtils.isNotEmpty(companyDOPageResult)){
            newList.addAll(companyDOPageResult);
        }
        List<CompanyRespDTO> companyRespDTOList=CompanyConvert.INSTANCE.convertList1(newList);
        for (CompanyRespDTO companyRespDTO:companyRespDTOList) {
//            String jobJson=companyRedisDAO.get(companyRespDTO.getId().toString());//获取缓存
//            if(StringUtils.isEmpty(jobJson)){
               List<MemberCompanyJobDO>  memberCompanyJobDOList=memberJobService.getJobListTop2(companyRespDTO.getId());
            String  jobJson=JsonUtils.toJsonString(memberCompanyJobDOList);
                companyRedisDAO.set(companyRespDTO.getId().toString(),jobJson,d);
//            }
            //有则json对象 设置进去
            companyRespDTO.setCompanyJobDOList(jobJson);
        }

        //插入职位信息
        return companyRespDTOList;
    }

    @Override
    public List<CompanyRespDTO> getCompanyByIds(List<Long> ids) {
        return CompanyConvert.INSTANCE.convertList1(companyService.getCompanyList(ids));
    }

    @Override
    public List<CompanyRespDTO> getCompanyAllByIds(List<Long> ids) {
        return CompanyConvert.INSTANCE.convertList1(companyService.getCompanyAllByIds(ids));
    }


    @Override
    public CompanyRespDTO getCompanyById(Long id) {
        CompanyRespDTO companyRespDTO=new CompanyRespDTO();
        CompanyDO companyDO=companyService.getCompany(id,1);
        if(null!=companyDO){
            BeanUtils.copyProperties(companyDO,companyRespDTO);
        }
        return companyRespDTO;
    }

    @Override
    public Boolean updateCompany(CompanyUpdateReqDTO companyUpdateReqDTO) {
        CompanyUpdateReqVO companyUpdateReqVO=new CompanyUpdateReqVO();
        BeanUtils.copyProperties(companyUpdateReqDTO,companyUpdateReqVO);
        companyService.updateCompany(companyUpdateReqVO);
        return true;
    }

    @Override
    public Boolean isAuthentication(String phone) {
        CompanyDO companyDO= companyService.getCompanyByPhone(phone);
        if(companyDO!=null&&companyDO.getAuthorizedStatus().equals("authorized")){
            return true;
        }
        return false;
    }

    @Override
    public Boolean isAuthenticationId(Long id) {
//        CompanyDO companyDO= companyService.getCompanyNotenant(id);
        CompanyUpdateReqVO companyUpdateReqVO=new CompanyUpdateReqVO();
        companyUpdateReqVO.setId(id);
        companyUpdateReqVO.setIsRealName("yes");
        companyService.updateCompany(companyUpdateReqVO);
        return true;
    }

    @Override
    public List<CompanyUserRespDTO> getCompanyUserByCompanyId(Long companyId) {
        CompanyUserPageReqVO pageReqVO=new CompanyUserPageReqVO();
        pageReqVO.setCompanyId(companyId);
        List<CompanyUserDO> companyUserDOList=companyService.getCompanyUserPage(pageReqVO).getList();
        return CompanyUserConvert.INSTANCE.convertList(companyUserDOList);
    }
}
